1 /* 2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021 3 License: [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License]. 4 Authors: Marcelo S. N. Mancini 5 6 Copyright Marcelo S. N. Mancini 2018 - 2021. 7 Distributed under the CC BY-4.0 License. 8 (See accompanying file LICENSE.txt or copy at 9 https://creativecommons.org/licenses/by/4.0/ 10 */ 11 module hip.asset; 12 13 import hip.api.data.commons; 14 15 /** Controls the asset ids for every game asset 16 * 0 is reserved for errors. 17 */ 18 private __gshared uint currentAssetID = 0; 19 20 abstract class HipAsset : ILoadable, IHipAsset 21 { 22 /** Use it to insert into an asset pool, alias*/ 23 protected string _name; 24 /**Currently not in use */ 25 protected uint _assetID; 26 /** Usage inside asset manager */ 27 protected uint _typeID; 28 29 ///When it started loading 30 float startLoadingTimestamp; 31 ///How much time it took to load, in millis 32 float loadTime; 33 34 this(string assetName) 35 { 36 this.name = assetName; 37 _assetID = ++currentAssetID; 38 } 39 40 string name() const{return _name;} 41 string name(string newName) {return _name = newName;} 42 uint assetID() const {return _assetID;} 43 uint typeID() const { return _typeID;} 44 45 /** 46 * Action for when the asset finishes loading 47 * Proabably be executed on the main thread 48 */ 49 abstract void onFinishLoading(); 50 51 52 void startLoading() 53 { 54 import hip.util.time; 55 startLoadingTimestamp = HipTime.getCurrentTimeAsMilliseconds(); 56 } 57 58 void finishLoading() 59 { 60 import hip.util.time; 61 if(isReady()) 62 { 63 onFinishLoading(); 64 loadTime = HipTime.getCurrentTimeAsMilliseconds() - startLoadingTimestamp; 65 } 66 } 67 68 /** 69 * Currently, no AssetID recycle is in mind. It will only invalidate 70 * the asset for disposing it on an appropriate time 71 */ 72 final void dispose() 73 { 74 _assetID = 0; 75 onDispose(); 76 } 77 ///Use it to clear the engine. 78 abstract void onDispose(); 79 } 80 81 82 class HipFileAsset : HipAsset 83 { 84 string path; 85 ubyte[] data; 86 this(in string path) 87 { 88 super("File_"~path); 89 this.path = path; 90 _typeID = assetTypeID!HipFileAsset; 91 } 92 void load(in ubyte[] data){this.data = cast(ubyte[])data;} 93 string getText(){return cast(string)data;} 94 override void onFinishLoading(){} 95 override void onDispose(){} 96 bool isReady(){return data.length != 0;} 97 } 98 99 private __gshared int assetIds = 0; 100 int assetTypeID(T : IHipAsset)() 101 { 102 __gshared int id = -1; 103 if(id == -1){id = ++assetIds;} 104 return id; 105 }